package com.wonders.hisdoctor.station.util;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wonders.his.common.service.project.base.domain.AppBaseParam;
import com.wonders.his.common.service.project.base.mapper.AppBaseParamMapper;
import com.wonders.his.common.service.utils.CountAgeUtil;
import com.wonders.his.common.utils.DateUtils;
import com.wonders.hisdoctor.station.constant.ServiceConstant;
import com.wonders.hisdoctor.station.enums.AppBaseParamTypeEnum;
import com.wonders.hisdoctor.station.project.base.domain.PatMasterIndex;
import com.wonders.hisdoctor.station.project.base.mapper.PatMasterIndexMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * Description: 常用工具类
 *
 * @author tong-aj
 * <p>
 * Create: 2021-05-25 10:30
 */
@Component
public class CommonUtil {

    private AppBaseParamMapper appBaseParamMapper;
    private PatMasterIndexMapper patMasterIndexMapper;

    @Autowired
    public void setAppBaseParamMapper(AppBaseParamMapper appBaseParamMapper) {
        this.appBaseParamMapper = appBaseParamMapper;
    }

    @Autowired
    public void setPatMasterIndexMapper(PatMasterIndexMapper patMasterIndexMapper) {
        this.patMasterIndexMapper = patMasterIndexMapper;
    }

    /**
     * Description: 处理处方打印标题
     * Param: [hospitalCode, printVo]
     * return: void
     * Author: tong-aj
     * Date: 2021/5/14
     */
    public String setPrintTitle(String hospitalCode) {
        // 标题
        AppBaseParam queryParam = new AppBaseParam();
        queryParam.setTypeValue(hospitalCode);
        queryParam.setType(ServiceConstant.HOSPITAL_LONG);
        queryParam.setKeyName(ServiceConstant.PRINT_TITLE);
        AppBaseParam appBaseParam = appBaseParamMapper.selectOne(queryParam);
        if(appBaseParam == null){
            return ServiceConstant.DEFAULT_TILE;
        } else {
            return appBaseParam.getValueName();
        }
    }

    /**
    * Description: 通用方法获取全局配置，如果机构的没有获取，则获取全局的
    * Param: [hospitalCode, keyName, type]
    * return: com.wonders.his.common.service.project.base.domain.AppBaseParam
    * Author: tong-aj
    * Date: 2021/6/4
    */
    public AppBaseParam getAppBaseParam(String hospitalCode,String keyName,Long type){
        AppBaseParam appBaseParam = new AppBaseParam();
        appBaseParam.setKeyName(keyName);
        appBaseParam.setType(type);
        appBaseParam.setTypeValue(hospitalCode);
        AppBaseParam result = appBaseParamMapper.selectOne(appBaseParam);
        if (result == null) {
            appBaseParam.setType(AppBaseParamTypeEnum.GLOBAL.getType());
            appBaseParam.setTypeValue("");
            result = appBaseParamMapper.selectOne(appBaseParam);
        }
        return result;
    }
    
    /**
    * Description: 根据patientId获得年龄
    * Param: [patientId]
    * return: java.lang.String
    * Author: tong-aj
    * Date: 2021/6/4
    */
    public String getAge(String patientId){
        // 根据patientId获取DATE_OF_BIRTH
        PatMasterIndex patMasterIndex = patMasterIndexMapper.selectOne(new QueryWrapper<PatMasterIndex>().eq(ServiceConstant.PATIENT_ID, patientId));
        Date dateOfBirth = patMasterIndex.getDateOfBirth();
        String formatTime = DateUtils.dateFormatTime(dateOfBirth, DateUtils.YYYY_MM_DD);
        return CountAgeUtil.getAgeStr(formatTime);
    }
    
    /**
    * Description: 获取类中，属性上，@TableField注解的值，即数据库字段名
    * Param: [clazz, fieldName]
    * return: java.lang.String
    * Author: tong-aj
    * Date: 2021/7/27
    */
    public String getFieldColumn(Class clazz,String fieldName){
        try {
            Field declaredField = clazz.getDeclaredField(fieldName);
            TableField declaredAnnotation = declaredField.getDeclaredAnnotation(TableField.class);
            return declaredAnnotation.value();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return "";
    }
}
